// pages/import/import.js
var util = require("../../../utils/util.js");
var app = getApp();
var moreup=false;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    values:'',
    work_order_id: '',
    user_id:'',
    scanning:'../../../images/scanning.png',
    showData: [{ device_no: '', core_num:0,add_num:0,sum_num:0}],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var id = options.id;
    that.setData({
      work_order_id: id,
      user_id:options.user_id
    })
    that.getDetail();//获取缴费器
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    moreup = false;
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  //获取缴费器
  getDetail(){
    var that = this;
    app.ajax({
      url: 'Engineer/Workorder/getStovesController',
      data: {
        work_order_id: that.data.work_order_id,
      },
      success: function (res) {
        if (res.data.code == 1000) {
          var data = res.data.data;
          data.forEach((item)=>{
            item.type = 1;
          })
          that.setData({
            showData: data,
          })
          // console.log("获取缴费器")
          // console.log(that.data.showData)
          that.sumnum();
        }
      }
    })
  },
  //计算总眼数
  sumnum(){
    var that = this;
    var showData = that.data.showData;
    showData.forEach((item)=>{
      item.sum_num = item.core_num
    })
    that.setData({
      showData,
    })
    console.log(that.data.showData)
    console.log("总眼数")
  },
  //缴费器输入框获取焦点
  stovesleave(e){
    var that = this;
    var value = e.detail.value;
    var index = e.currentTarget.dataset.index;
    var showData = that.data.showData
    if(value == 0){
      showData[index].add_num = ""
      that.setData({
        showData
      })
    }
  },
  //失去焦点后计算总眼数
  inputSum(e){
    var that = this;
    var value = e.detail.value;
    var index = e.currentTarget.dataset.index
    var showData = that.data.showData
    showData[index].sum_num = Number(e.detail.value) + Number(showData[index].core_num);
    if(value == ""){
      showData[index].add_num = 0
      this.setData({
        showData
      })
    }
    else{
      showData[index].add_num = e.detail.value;
      this.setData({
        showData
      })
    }
  },
  //添加类型
  addDevice(){
    var obj = { device_no: '', core_num: '',add_num:0,sum_num:0, core_type: '', currindex: '' };
    var showData = this.data.showData;
    showData.push(obj);
    this.setData({
      showData
    })
  },
  // 跳转缴费器调试
  goDebug(){
    wx.navigateTo({
      url: '/pages/subpackage/pages/device/test/test',
    })
  },
  // 跳转提示页面
  goDebugTips(){
    wx.navigateTo({
      url: '../../debugTips/debugTips',
    })
  },
  //input 输入框
  inputDevice(e){
    var that=this;
    var index = e.currentTarget.dataset.index;
    var showData = this.data.showData;
    showData[index].device_no=e.detail.value
    that.setData({
      showData
    })
  },
  //数量
  inputNumber(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var showData = this.data.showData;
    showData[index].core_num = e.detail.value
    that.setData({
      showData
    })
  },
  // 扫码
  scanning: function(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    var showData = this.data.showData;
    app.scanning(function (data) {
      showData[index].device_no = data;
      that.setData({
        showData
      })
    })
    // wx.scanCode({
    //   success: (res) => {

    //     console.log(res)
    //     var code = res.result.split(';')
    //     if (!util.isExitsVariable(code)||code.length==0){
    //       app.showToast('扫码失败');
    //       return false;
    //     }
    //     var val = code[2].split(':');
    //     if (!util.isExitsVariable(val)||val.length==0) {
    //       app.showToast('扫码失败');
    //       return false;
    //     }
    //     that.setData({
    //       values: val[1]
    //     })

    //   },
    //   fail: (res) => {
    //     app.showToast('扫码失败，请重试！');
    //   }
    // })
  },
  //删除缴费器
  deleteFun(e){
    var that=this;
    var index = e.currentTarget.dataset.index;
    var showData = that.data.showData;
    wx.showModal({
      title: '是否删除该选项?',
      content: '',
      confirmColor: '#DF0101',
      success(res) {
        if (res.confirm) {
          showData.splice(index, 1);
          that.setData({ showData })
        } else if (res.cancel) {

        }

      }
    })

  },
  //确认事件 缴费器信息
  affirmfun(e){
    var that = this;
    var showData = that.data.showData;
    var cando=true;
    showData.forEach((item,index)=>{
      item.core_num = item.sum_num;
      if (item.device_no==''){
          app.showToast("请输入第 "+ (index+1) +" 个缴费器编号", "none", 2000, function () { });
            cando = false;
            return false;
        }
      // if (item.add_num==''){
      //   app.showToast("请输入第 " + (index + 1) + " 个炉具个数", "none", 2000, function () { });
      //     cando = false;
      //     return false;
      // }
      if (item.sum_num <= 0) {
        app.showToast("第 " + (index + 1) + " 个炉具个数不能小于1", "none", 2000, function () { });
        console.log(item.sum_num)
        cando = false;
        return false;
      }
      // if (item.currindex=='') {
      //   app.showToast("请选择第 " + (index + 1) + " 个炉具类型", "none", 2000, function () { });
      //     cando = false;
      //     return false;
      // }
      
    })
    if (cando == false){
      console.log('进来没参数', showData)
      return false;
    }
    console.log('提交的数', showData)
    if(moreup){
      console.log('??', moreup)
      return false
    }
    moreup=true;
    var work_order_id = that.data.work_order_id;
    that.setData({showData})
    app.ajax({
      url: 'Engineer/Workorder/bindEquipment',
      data: {
        work_order_id,
        device_information:showData},
      success: function(res){
        showData.forEach((item,index)=>{
          showData[index].core_num = 0;
        })
        that.setData({
          showData
        })
        if(res.data.code == 1000){
         
          app.showToast("绑定成功","none",2000,function(){});
          var timer;
          timer = setTimeout(function () {
            moreup=false
            wx.navigateBack({
              delta: 1
            })
          }, 1000);
        } else if (res.data.code == 2020){
           app.showToast(res.data.message[0], "none", 2000, function (){});
            if(res.data.code==-1201){
              //如果未进行实名跳转到实名页面
              that.toRealname();
            }
        } else {
          app.showToast(res.data.message, "none", 2000, function () { });
        }
        setTimeout(()=>{
          moreup = false
        },1000)

      }
    })

  },
  //跳转到实名
  toRealname(){
    var that = this;
      app.ajax({
        url: "Engineer/Realname/getAuthInfo",
        data: { user_id: that.data.user_id },
        success: function (res) {
          if (res.data.code == 1000 && res.data.data.status == -2) {
            wx.navigateTo({
              url: '../../realname/uploadDocuments/uploadDocuments?user_id=' + that.data.user_id,
            })
          } else if (res.data.code == 1000 && res.data.data.status == -3) {
            wx.navigateTo({
              url: '../../realname/faceRecognition/faceRecognition?user_id=' + that.data.user_id,
            })
          } else if (res.data.code == 1000 && (res.data.data.status == 2 || res.data.data.status == 1)) {
            wx.navigateTo({
              url: '../../realname/myCertification/myCertification?user_id=' + that.data.user_id,
            })
          } else {
            wx.navigateTo({
              url: '../../realname/realName/realName?user_id=' + that.data.user_id,
            })
          }
        }


      })

  }

})
